/********************************************************************************
* File Name:	mainwindow.cpp
* Description:	第3章 实例3：Command Link Button的实例
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include<QFileDialog>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->currentImage = 0;
    this->list<<":/images/1.jpg"<<":/images/2.jpg"<<":/images/3.jpg"<<":/images/4.jpg"<<":/images/5.jpg";

    ui->imageLabel->setBackgroundRole(QPalette::Base);
    ui->imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    ui->imageLabel->setScaledContents(true);
    resize(500, 400);

    QString fileName = list.at(this->currentImage);
    ui->imageLabel->setText(fileName);
    if (!fileName.isEmpty())
    {
        QImage image(fileName);
        if (image.isNull())
        {

            QMessageBox::information(this, tr("Image Viewer"),
                                         tr("Cannot load %1.").arg(fileName));
            return;
        }
        ui->imageLabel->setPixmap(QPixmap::fromImage(image));
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_commandLinkButtonNext_clicked()
{
    if(this->currentImage == 4)
        this->currentImage =0;
    else
        this->currentImage++;
    QString fileName = list.at(this->currentImage);
    ui->imageLabel->setText(fileName);
    if (!fileName.isEmpty())
    {
        QImage image(fileName);
        if (image.isNull())
        {

            QMessageBox::information(this, tr("Image Viewer"),
                                         tr("Cannot load %1.").arg(fileName));
            return;
        }
        ui->imageLabel->setPixmap(QPixmap::fromImage(image));
    }
}
